import os
import json
import config
import requests

class model_chatgpt_3p5_turbo:
    def __init__(self) -> None:
        super(model_chatgpt_3p5_turbo, self).__init__()
        for llm in config.get_config().models.llms:
            if llm.name == "AiSkt" and llm.enable == 1:
                self._gpt3p5_path = llm.path

    def completion(self, prompt_system, prompt_user):
        api_url = self._gpt3p5_path + '/chat/completions'
        api_key = os.environ.get('CHAT_GPT_APIKEY')
        headers = {
            'Authorization': f'Bearer {api_key}',
            'Content-Type': 'application/json'
        }
        payload = {
            'model': "gpt-3.5-turbo",
            'messages': [{"role": "system", "content": prompt_user}]
        }
        try:
            response = requests.post(api_url, headers=headers, data=json.dumps(payload))
            if response.status_code == 200:
                data = response.json()
                return data['choices'][0]['message']['content']
            else:
                return f'Error: Received status code {response.status_code}'
        except Exception as e:
            print(e)
            return 'An error occurred while sending the request'